package com.gs.gaosheng.service.impl;

import com.gs.gaosheng.domain.GsCouponUser;
import com.gs.gaosheng.domain.User;
import com.gs.gaosheng.mapper.GsCouponUserMapper;
import com.gs.gaosheng.mapper.UserMapper;
import com.gs.gaosheng.service.GsDrawService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class GsDrawServiceImpl implements GsDrawService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GsCouponUserMapper gsCouponUserMapper;

    /**
     * 抽奖程序
     * @param userAccount 用户账号
     * @return 抽奖结果
     */
    @Override
    public Integer draw(String userAccount) {
        Long userScore = userMapper.selectUserScore(userAccount);
        Long userId = userMapper.selectUserIdByAccount(userAccount);

        //抽奖结果对应：
        // 0 -> 谢谢惠顾,
        // 1 -> 积分+20,
        // 2 -> 积分+100,
        // 3 -> 积分+20,
        // 4 -> 10元优惠券,
        // 5 -> 积分+20,
        // 6 -> 5元优惠券,
        // 7 -> 积分+20,
        // 8 -> 积分不足
        Long[] number = {25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L};
        Random random = new Random();
        int a = random.nextInt(7);
        GsCouponUser gsCouponUser = new GsCouponUser();
        System.out.println("before score :" + userScore);
        if (userScore >= 100) {
            userScore -= 100;
            if (number[a] == 26 || number[a] == 28 || number[a] == 30 || number[a] == 32) {
                userScore += 20;
                userMapper.updateUserScore(userAccount, userScore);
                return a;
            } else if (number[a] == 27) {
                userScore += 100;
                userMapper.updateUserScore(userAccount, userScore);
                return a;
            } else if (number[a] == 29) {
                gsCouponUser.setUserId(userId);
                gsCouponUser.setCouponId(2L);
                gsCouponUser.setCouponStatus("0");
                gsCouponUserMapper.insertGsCouponUser(gsCouponUser);
                userMapper.updateUserScore(userAccount, userScore);
                return a;
            } else if (number[a] == 31) {
                gsCouponUser.setUserId(userId);
                gsCouponUser.setCouponId(1L);
                gsCouponUser.setCouponStatus("0");
                gsCouponUserMapper.insertGsCouponUser(gsCouponUser);
                userMapper.updateUserScore(userAccount, userScore);
                return a;
            } else if (number[a] == 25) {
                userMapper.updateUserScore(userAccount, userScore);
                return a;
            }
        } else {
            return 8;
        }
        return 9;
    }
}
